﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using System.Collections;

namespace School.Service.Clothes
{
    public class SRV_Clothe_Sizes_Size
    {
        private static SchoolEntities db = new SchoolEntities();
        public static IEnumerable<SRV_Clothe_Sizes> AllClothesSizes()
        {
            var query = from Clothe in db.SRV_Clothe_Sizes select Clothe;
            return query.ToList();
        }
        public static IEnumerable<object> AllClothesSizesByClotheId(int ClotheId)
        {
            var query = from Size in db.SRV_Clothe_Sizes
                        where Size.Clothe_Id == ClotheId
                        select new
                       {
                           Size_Id = Size.Size_Id,
                           Code = Size.SRV_Clothe.Code,
                           Descrption = Size.SRV_Clothe.Description,
                           SizeName = Size.Name,
                           Price = (double?)Size.SRV_Clothe_Price.Where(s => s.Size_Id == Size.Size_Id).OrderByDescending(s => s.Price_Id).FirstOrDefault().Price
                       };

            return query.ToList();
        }
        public static IEnumerable<SRV_Clothe_Sizes> AllSizesByClotheId(int ClotheId)
        {
            var query = from Size in db.SRV_Clothe_Sizes
                        where Size.Clothe_Id == ClotheId
                        select Size;
            return query.ToList();
        }
        public static bool ClothesSizeInsert(SRV_Clothe_Sizes Clothe)
        {
            db.AddToSRV_Clothe_Sizes(Clothe);
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ClothesSizeUpdate(List<SRV_Clothe_Sizes> ClotheSizesToUpdate)
        {
            foreach (SRV_Clothe_Sizes item in ClotheSizesToUpdate)
            {
                db.SRV_Clothe_Sizes.Where(s => s.Size_Id == item.Size_Id).First().QTY = item.QTY;
            }
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ClothesSizeDelete(IEnumerable<SRV_Clothe_Sizes> ListSizes)
        {
            foreach (var item in ListSizes)
            {
                db.SRV_Clothe_Sizes.DeleteObject(item);
            }

            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ClotheSizeAddRange(List<SRV_Clothe_Sizes> SizesList)
        {
            IEnumerable<SRV_Clothe_Sizes> lstClothesSizes = AllClothesSizes().Where(s => s.Clothe_Id == SizesList.First().Clothe_Id);
            foreach (SRV_Clothe_Sizes item in lstClothesSizes)
            {
                db.SRV_Clothe_Sizes.DeleteObject(item);
            }
            foreach (var item in SizesList)
            {
                db.AddToSRV_Clothe_Sizes(item);
            }
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IEnumerable<object> SizeSearch(int campusid, int cerId, int StageId, int GradeId)
        {

            var query = from Size in db.SRV_Clothe_Sizes
                        where Size.SRV_Clothe.Campus_Id == campusid && Size.SRV_Clothe.Cer_Id == cerId
                        && Size.SRV_Clothe.Stage_Id == StageId && Size.SRV_Clothe.Grad_Id == GradeId
                        select new
                        {
                            SizeId = Size.Size_Id,
                            Size_Code = Size.Size_Code,
                            Name = Size.SRV_Clothe.Name,
                            ClotheCode = Size.SRV_Clothe.Code,
                            SizeName = Size.Name,
                            Pysical_Qty = "",
                            Differance = "",
                            SystemQty = Size.QTY
                        };
            return query.ToList();
        }
        public static IQueryable<SizeReport> SizeStockReport(int campusid, int cerId, int StageId, int GradeId)
        {
            var query = from Size in db.SRV_Clothe_Sizes
                            where Size.SRV_Clothe.Campus_Id == campusid && Size.SRV_Clothe.Cer_Id == cerId
                            && Size.SRV_Clothe.Stage_Id == StageId && Size.SRV_Clothe.Grad_Id == GradeId
                            select new SizeReport
                            {
                                SizeId = Size.Size_Id,
                                Size_Code = Size.Size_Code.TrimEnd(),
                                Name = Size.SRV_Clothe.Name.TrimEnd(),
                                ClotheCode = Size.SRV_Clothe.Code.TrimEnd(),
                                SizeName = Size.Name.TrimEnd(),
                                SystemQty = Size.QTY
                            };
                return query;
            }
    }
    public class SizeReport
    {
        public long SizeId { get; set; }
        public string Size_Code { get; set; }
        public string Name { get; set; }
        public string ClotheCode { get; set; }
        public string SizeName { get; set; }
        public long SystemQty { get; set; }
    }
}
